package com.zjdiepu.www.biz.third;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.third.AliDeliverInfoDetailMapper;
import com.zjdiepu.www.ibiz.third.IAliDeliverInfoDetailService;
import com.zjdiepu.www.model.third.dto.AliDeliverInfoDetail;
import com.zjdiepu.www.model.third.ext.AliDeliverInfoDetailExt;

/**
 * 快递路径详情Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月18日 14时15分<br/>
 * @since 1.0<br/>
 */
@Service
public class AliDeliverInfoDetailServiceImpl extends BaseService implements IAliDeliverInfoDetailService {
	public static final Logger logger = LoggerFactory.getLogger(AliDeliverInfoDetailServiceImpl.class);

	@Autowired
	private AliDeliverInfoDetailMapper aliDeliverInfoDetailMapper;
	
	@Override
	public void saveUpdateAndDelete(AliDeliverInfoDetail aliDeliverInfoDetail, OperateType operType) {
		switch (operType) {
		case add:
			aliDeliverInfoDetail.initInsertData(aliDeliverInfoDetail.getModifier(), aliDeliverInfoDetail.getModifierName());
			aliDeliverInfoDetailMapper.insert(aliDeliverInfoDetail);
			break;
		case edit:
			aliDeliverInfoDetail.initUpdateData(aliDeliverInfoDetail.getModifier(), aliDeliverInfoDetail.getModifierName());
			// 影响的记录数
			int updateCount = aliDeliverInfoDetailMapper.updateByPrimaryKeySelective(aliDeliverInfoDetail);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			aliDeliverInfoDetail.initDeleteData(aliDeliverInfoDetail.getModifier(), aliDeliverInfoDetail.getModifierName());
			// 影响的记录数
			int deleteCount = aliDeliverInfoDetailMapper.updateByPrimaryKeySelective(aliDeliverInfoDetail);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<AliDeliverInfoDetail> selectByExt(AliDeliverInfoDetailExt aliDeliverInfoDetailExt) {
		return aliDeliverInfoDetailMapper.selectByExt(aliDeliverInfoDetailExt);
	}
	
	@Override
	public int countByExt(AliDeliverInfoDetailExt aliDeliverInfoDetailExt){
		return aliDeliverInfoDetailMapper.countByExt(aliDeliverInfoDetailExt);
	}
	
	@Override
	public AliDeliverInfoDetail selectById(String id) {
		return aliDeliverInfoDetailMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			aliDeliverInfoDetailMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(AliDeliverInfoDetail aliDeliverInfoDetail) {
		return aliDeliverInfoDetailMapper.countRefined(aliDeliverInfoDetail);
	}

	@Override
	public AliDeliverInfoDetail selectRefined(AliDeliverInfoDetail aliDeliverInfoDetail) {
		return aliDeliverInfoDetailMapper.selectRefined(aliDeliverInfoDetail);
	}

	@Override
	public boolean repeat(AliDeliverInfoDetail aliDeliverInfoDetail) {
		int count = countRefined(aliDeliverInfoDetail);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(aliDeliverInfoDetail.getId())) {
			return true;
		}
		AliDeliverInfoDetail dbItem = selectRefined(aliDeliverInfoDetail);
		if(null == dbItem) {
			return false;
		}
		return !aliDeliverInfoDetail.getId().equals(dbItem.getId());
	}

	@Override
	public void deleteByInfoId(String deliverInfoId) {
		aliDeliverInfoDetailMapper.deleteByInfoId(deliverInfoId);
	}

	@Override
	public int insertBatch(List<AliDeliverInfoDetail> aliDeliverInfoDetails) {
		if(!CollectionUtils.isEmpty(aliDeliverInfoDetails)) {
			return aliDeliverInfoDetailMapper.insertBatch(aliDeliverInfoDetails);
		}
		return 0;
	}
}
